﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.OleDb;

namespace DAO
{
    public class DHDAO
    {
        public List<DatHangDTO> LayDonDHChuaNhap()
        {
            string sql = " SELECT DISTINCT ct.MaDH FROM CTDatHang ct WHERE ct.ConLai > 0 " ;
            OleDbConnection conn = DataProvider.ConnectDB();
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            OleDbDataReader reader = cmd.ExecuteReader();
            List<DatHangDTO> danhsach = new List<DatHangDTO>();
            while (reader.Read())
            {
                DatHangDTO dhDTO = new DatHangDTO();
                dhDTO.MaDH = reader.GetInt32(0);
                dhDTO.NgayDat = reader.GetDateTime(1);
                dhDTO.MaNV = reader.GetInt32(2);
                dhDTO.TrangThai = reader.GetBoolean(3);
                dhDTO.GhiChu = reader.GetString(4);
                dhDTO.Diem = reader.GetInt32(5);
                danhsach.Add(dhDTO);
            }
            conn.Close();
            return danhsach;

        }

        public List<DatHangDTO> LayMaDonHangTrongPhieuNhap(int ma)
        {
            string sql = " SELECT * FROM DatHang DH WHERE DH.MaDH IN ( SELECT PN.MaDH FROM PhieuNhap PN WHERE MaPN = @ma ) ";
            OleDbConnection conn = DataProvider.ConnectDB();
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            OleDbParameter para = cmd.Parameters.Add("@ma", OleDbType.Integer);
            para.Value = ma;
            OleDbDataReader reader = cmd.ExecuteReader();
            List<DatHangDTO> danhsach = new List<DatHangDTO>();

            while (reader.Read())
            {
                DatHangDTO dhDTO = new DatHangDTO();
                dhDTO.MaDH = reader.GetInt32(0);
                dhDTO.NgayDat = reader.GetDateTime(1);
                dhDTO.MaNV = reader.GetInt32(2);
                dhDTO.TrangThai = reader.GetBoolean(3);
                dhDTO.GhiChu = reader.GetString(4);
                dhDTO.Diem = reader.GetInt32(5);
                danhsach.Add(dhDTO);

            }
            conn.Close();
            return danhsach;

        }
        public OleDbDataAdapter LayMaDHChuaHoanThanh()
        {
            string sql = " SELECT DISTINCT ct.MaDH FROM CTDatHang ct WHERE ct.ConLai > 0 ";
            OleDbConnection conn = DataProvider.ConnectDB();
            OleDbCommand cmd = new OleDbCommand(sql, conn);
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            conn.Close();
            return da;
        }             
    }
}
